<?php

namespace App\Admin\Extensions;

use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Admin;

class MediaCheckRowController extends RowAction
{
    /**
     * 返回字段标题
     *
     * @return string
     */
    public function title()
    {
        return '审核';
    }

    /**
     * 添加JS
     *
     * @return string
     */
    protected function script()
    {
        return <<<JS
$('.grid-check-row').on('click', function () {
    var id = $(this).data('id');
    var aid = $(this).data('aid');
    layer.confirm('审核操作？', {
      btn: ['通过','不通过'] //按钮
    }, function(){
      $.ajax({
      url: "postAudit",
      data: {'id':id,'aid':aid},
      success: function(data){
        console.log(data)
        if(data.status==1){
            layer.msg(data.msg, {icon: 1});
            setTimeout(function () {
                Dcat.reload();
            }, 2000);
        }else{
            layer.msg(data.msg, function(){});
        }
      }});
    }, function(){
        layer.prompt({title: '请输入不通过原因！', formType: 2}, function(text, index){
            layer.close(index);
            $.ajax({
              url: "postAudit",
              data: {'id':id,'aid':aid,'reason':text},
              success: function(data){
                if(data.status==1){
                    layer.msg(data.msg, {icon: 1});
                    setTimeout(function () {
                        Dcat.reload();
                    }, 2000);
                }else{
                    layer.msg(data.msg, function(){});
                }
            }});
        });
    });
    // Your code.
    console.log($(this).data('id'));
    console.log($(this).data('aid'));

});
JS;
    }

    public function html()
    {
        // 获取当前行数据ID
        $id = $this->getKey();

        // 获取当前行数据的用户名
        // $username = $this->row->username;
        // print_r($id);die;
        // 这里需要添加一个class, 和上面script方法对应
        $this->setHtmlAttribute(['data-id' => $id, 'data-aid'=>Admin::user()->id, 'class' => 'grid-check-row']);

        return parent::html();
    }
}